<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0">左边占位</el-col>
      <el-col :span="12" :xs="24">
        <el-form ref="refForm" class="login_form" :model="loginForm" :rules="rules">
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" placeholder="请输入用密码" :prefix-icon="Lock" v-model="loginForm.password" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="login_btn" type="primary" size="default" @click="login" :loading="loading">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import  { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import useUserStore from '@/store/modules/user'
import { getTime } from '@/utils/time'

const $router = useRouter()
const $route = useRoute()

let userSore = useUserStore()
let loading = ref(false)
const refForm = ref()
const validateUsername = (rule: any, value: string, callback: any) => {
  if (!value.length) {
    callback(new Error('用户名不能为空'))
  } else {
    callback()
  }
}
let rules = reactive({
  username: [
    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'change' },
    { validator: validateUsername, trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'change' }
  ]
})

// 表单数据
let loginForm = reactive({
  username: '',
  password: ''
})
// 登录函数
const login = async () => {
  // 表单校验
  await refForm.value.validate()
  loading.value = true
  try {
    const result = await userSore.userLogin(loginForm)
    console.log(40, result)
    ElNotification({
      type: 'success',
      message: '登录成功',
      title: `${getTime()}好，欢迎回来`
    })
    // 判断登录后路由路径中是否含有redirect参数，如果有，则跳转到对应的路由地址，否则跳转到首页
    $router.push($route.query.redirect as string || '/')
  } catch (error: any) {
    console.log(53, error)
    ElNotification({
      type: 'error',
      message: error.message
    })
  } finally {
    loading.value = false
  }
}

</script>
<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 40px;
    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }
    .login_btn {
      width: 100%;
    }
  } 
}

</style>